package IOperations;

import method.createNum;
import method.isBigger;
import method.isDataSafe;
import node.Node;
import java.util.Scanner;

public class Division {
    public static void work() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入被除数整数：");
        String num1 = scanner.nextLine();
        if(!isDataSafe.work(num1)) {
            return;
        }
        System.out.print("请输入除数整数：");
        String numt=scanner.nextLine();
        String num2=new String();
        if(!isDataSafe.work(numt)) {
            return;
        }
        if(numt.equals("0")) {
            System.out.println("除数不能为0");
            return;
        }
        //处理符号
        boolean sign=true;
        if(numt.charAt(0)=='-'&&num1.charAt(0)!='-') {
            sign=false;
            numt=numt.substring(1,numt.length());
        }
        if(numt.charAt(0)!='-'&&num1.charAt(0)=='-') {
            sign=false;
            num1=num1.substring(1,num1.length());
        }
        num2=num2.concat(numt);

        //判断被除数是否大于除数
        if(!num1.equals(num2)&&!isBigger.work(num1,num2)) {
            System.out.println(0);
            return;
        }

        //将除数变为和被除数一样的位数
        int lenNum1=num1.length();
        int lenNum2=num2.length();
        int diff=lenNum1-lenNum2;//因为被除数一定大于等于除数
        if(diff!=0) {
            while(diff!=0) {
                num2=num2.concat("0");
                diff--;
            }
        }

        //
        String ret = new String();
        while(num2.length()>=numt.length()) {
            boolean flag=true;
            //如果num1比num2大或者相等就传入减法开减
            if(num1.equals(num2)||isBigger.work(num1,num2)) {
                flag=false;
                int count=0;
                while(num1.equals(num2)||isBigger.work(num1,num2)) {
                    num1 = Subtraction.divWork(num1, num2);
                    //减完后num1可能头部有0，需要处理
                    int i=0;
                    while(num1.charAt(i)=='0') {
                        if(i==num1.length()-1) {
                            break;
                        }
                        i++;
                    }
                    num1=num1.substring(i,num1.length());
                    count++;
                }
                ret=ret.concat(String.valueOf(count));
            }
            num2=num2.substring(0,num2.length()-1);
            if(flag==true) {
                ret=ret.concat("0");
            }
        }
        //处理ret头部0
        int i=0;
        while(ret.charAt(i)=='0') {
            if(i==ret.length()-1) {
                break;
            }
            i++;
        }
        ret=ret.substring(i,ret.length());
        //处理符号
        if(sign==false) {
            ret="-".concat(ret);;
        }
        System.out.println(ret);
    }
}
